package com.jd.ty.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.googlecode.jsonplugin.annotations.JSON;

import com.jd.ty.util.Pager;
import com.jd.ty.web.interceptor.LoginInterceptor;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.pojo.User;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements SessionAware {
	protected Logger log = Logger.getLogger(BaseAction.class);

	protected int page;
	private int rows;
	private String sidx;
	private String sord;
	protected Pager pager;
	
	protected Map session;

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSortStr() {
		HqlCriterions hql = new HqlCriterions();
		if(null==sidx||null==sord){
			return null;
		}
		hql.sort(sidx, sord);
		return hql.getSortString();
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSidx() {
		return sidx;
	}

	public String getSord() {
		return sord;
	}
	@JSON(serialize=false)
	public Pager getPagerObject(){
		pager=new Pager(rows,page);
		return pager;
	}
	@JSON(serialize=false)
	public User getUser(){
		return (User)session.get(LoginInterceptor.USER_SESSION_KEY);
	}

	public void setSession(Map<String, Object> session) {
		this.session=session;
		
	}
	private String returnUrl;  
	  
	public String getReturnUrl() {  
	    return returnUrl;  
	}  
	  
	public void setReturnUrl(String returnUrl) {  
	    this.returnUrl = returnUrl;  
	}
	public Map getErrorMessage(){
		Map ep=new HashMap();
		ep.put("isError", true);
		ep.put("msg", getActionMessages());
		clearMessages();
		return ep;
	}
	public Map getMessage(){
		Map ep=new HashMap();
		ep.put("isMsg", true);
		ep.put("msg", getActionMessages());
		clearMessages();
		return ep;
	}
	@Override
	public void addActionError(String anErrorMessage) {
		addActionMessage(anErrorMessage);
	}
}
